
package com.gw.business.statistics.exception.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.gw.business.analysis.model.UserTrackDataDetail;
import com.gw.business.analysis.service.ExceptionTrackService;
import com.gw.business.analysis.service.UserTrackDataDetailService;
import com.gw.business.common.util.DateUtil;
import com.gw.business.common.util.EmailConfig;
import com.gw.business.system.semil.model.EmailSetModel;
import com.gwghk.gaims.mail.client.SendingEmailClient;

/**
 * 用户注册异常
 * 
 * @author kidy
 * @since 1.0
 * @version 2015年3月10日 kidy
 */
@Component
public class UserRegistExceptionInfoTask {
    
    /** 日志对象 */
    private static final Logger LOGGER = LoggerFactory.getLogger(UserRegistExceptionInfoTask.class);
    
    @Resource
    public UserTrackDataDetailService userTrackDataDetailService;
    
    @Resource
    public ExceptionTrackService exceptionTrackService;
    
    // 每个30分钟执行一次
    @Scheduled(cron = "0 30 0 * * ?")
    public void execute() throws Exception {
        LOGGER.info("UserExceptionInfoTask start");
        
        try {
            
            String startTime = DateUtil.getDateFormat(DateUtil.addMinutes(new Date(), -30), "yyyy-MM-dd HH:mm:ss");
            String endTime = DateUtil.getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
            String nowTime = DateUtil.getDateFormat(new Date(), "yyyy-MM-dd ");
            String emails = "";
            String bufferContent="";
            List<String>userIds=new ArrayList<String>();
            // 获取注册异常信息
            List<UserTrackDataDetail> registExceptionList = userTrackDataDetailService.queryRegistExceptionData(startTime, endTime, null);
            if (null != registExceptionList && registExceptionList.size() > 0) {
                for (UserTrackDataDetail userTrackDataDetail : registExceptionList) {
                    List<UserTrackDataDetail> sendList =
                        userTrackDataDetailService.queryRegistExceptionSendData(nowTime, userTrackDataDetail.getUserId());
                    if (sendList == null || sendList.size() == 0) {
                        //用户异常信息当天未发送邮件
                        userIds.add(userTrackDataDetail.getUserId());
                      
                    }
                }
                if(userIds!=null&&userIds.size()>0){
                 bufferContent=DateUtil.getDateFormat(registExceptionList.get(0).getStartTime(),"yyyy-MM-dd HH:mm:ss")+"  "+"注册异常";
                List<EmailSetModel> emailList = exceptionTrackService.queryEmail("true");
                if (null != emailList) {
                    
                    if(bufferContent!=null)
                        for (String userId : userIds) {
                            //修改发送邮件状态
                            userTrackDataDetailService.updateUserRegistExceptionSend(userId, nowTime);
                        }
                        for (EmailSetModel emailSetModel : emailList) {
                            StringBuffer buf = new StringBuffer();
                            buf.append("<p>您好："+emailSetModel.getUserName()+"</p><p>用户访问追踪系统发现用户："+registExceptionList.get(0).getUserId()+",IP:"+registExceptionList.get(0).getIp()+"有注册异常情况,欲查看详细请登录用户访问追踪系统</p>");
                            // subject 主题 content 内容 fromEmail 发件箱地址 fromEmailDisplayName 别名 toEmail 收件箱
                           SendingEmailClient.sendMail(EmailConfig.getValue("sendMailSimple_goldMailApiUrl"), "GWFX", bufferContent, buf.toString(), null, false,
                                EmailConfig.getValue("fromEmail"), "", emailSetModel.getEmail(), null);
                          
                        }
                    
                    
                    
                }
            }
          
            }
        } catch (Exception e) {
            LOGGER.error("异常信息查询出错，错误信息：" + e.getMessage());
        }
        LOGGER.info("UserExceptionInfoTask end");
    }
    
}
